import { createRouter, createWebHashHistory } from "vue-router";
import { mainStore } from "../store/index.js";
import { ElNotification } from 'element-plus';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            redirect: {
                name: "Login"
            }
        },
        {
            path: "/Login",
            name: "Login",
            component: () => import("../views/Login.vue"),

        },
        {
            path: "/Manager",
            name: "Manager",
            component: () => import("../views/Manager.vue"),
            children: [
                {
                    name: "DataView",
                    path: "DataView",
                    component: () => import("../views/DataView.vue"),
                    meta: {
                        title: "首页",
                        requireAuth: true
                    }
                },
                {
                    path: "CategoryInfoList",
                    name: "CategoryInfoList",
                    component: () => import("../views/CategoryInfo/CategoryInfoList.vue"),
                    meta: {
                        title: "分类列表",
                        requireAuth: true
                    }
                },
                {
                    path: "AddCategoryInfo",
                    name: "AddCategoryInfo",
                    component: () => import("../views/CategoryInfo/AddCategoryInfo.vue"),
                    meta: {
                        title: "新增分类",
                        autoClose: true,
                        requireAuth: true
                    }
                },

                {
                    path: "CommtInfoList",
                    name: "CommtInfoList",
                    component: () => import("../views/commtInfo/CommtInfoList.vue"),
                    meta: {
                        title: "评论列表",
                        requireAuth: true
                    }
                },
                {
                    path: "FoodInfoList",
                    name: "FoodInfoList",
                    component: () => import("../views/foodInfo/FoodInfoList.vue"),
                    meta: {
                        title: "菜品列表",
                        requireAuth: true
                    }
                },
                {
                    path: "AddFoodInfo",
                    name: "AddFoodInfo",
                    component: () => import("../views/foodInfo/AddFoodInfo.vue"),
                    meta: {
                        title: "新增菜品",
                        autoClose: true,
                        requireAuth: true
                    }
                },

                {
                    path: "UserInfoList",
                    name: "UserInfoList",
                    component: () => import("../views/userInfo/UserInfoList.vue"),
                    meta: {
                        title: "用户信息列表",
                        requireAuth: true
                    }
                },
                {
                    path: "AddressInfoList",
                    name: "AddressInfoList",
                    component: () => import("../views/addressInfo/AddressInfoList.vue"),
                    meta: {
                        title: "地址信息列表",
                        requireAuth: true
                    }
                },
                {
                    path: "OrderInfoList",
                    name: "OrderInfoList",
                    component: () => import("../views/orderInfo/OrderInfoList.vue"),
                    meta: {
                        title: "订单列表",
                        requireAuth: true
                    }
                },
                {
                    path: "AdminInfoList",
                    name: "AdminInfoList",
                    component: () => import("../views/adminInfo/AdminInfoList.vue"),
                    meta: {
                        title: "管理员列表",
                        requireAuth: true
                    }
                },
                {
                    path: "EditFoodInfo/:id",
                    name: "EditFoodInfo",
                    component: () => import("../views/foodInfo/EditFoodInfo.vue"),
                    meta: {
                        title: "编辑菜品",
                        autoClose: true,
                        requireAuth: true

                    }
                },

                {
                    path: "EditCategoryInfo/:id",
                    name: "EditCategoryInfo",
                    component: () => import("../views/CategoryInfo/EditCategoryInfo.vue"),
                    meta: {
                        title: "编辑分类",
                        autoClose: true,
                        requireAuth: true
                    }

                },
                {
                    path: "EditAdminInfo/:id",
                    name: "EditAdminInfo",
                    component: () => import("../views/adminInfo/EditAdminInfo.vue"),
                    meta: {
                        title: "编辑管理员",
                        autoClose: true,
                        requireAuth: true

                    }
                },
                {
                    path: "AddAdminInfo",
                    name: "AddAdminInfo",
                    component: () => import("../views/adminInfo/AddAdminInfo.vue"),
                    meta: {
                        title: "编辑管理员",
                        autoClose: true,
                        requireAuth: true

                    }
                },

            ]
        }
    ]
});

// 导航守卫 (路由的钩子函数)
// beforeEach跳转页面之前
router.beforeEach((to, from, next) => {
    const store = mainStore();
    if (to.meta.requireAuth) {
        //需要登录以后才能进
        if (store.token) {
            //说明有token，放行
            next();
        } else {
            ElNotification({
                type: "warning",
                title: "系统提示",
                message: "您还没有登录，请选去登录"
            });
            next({
                name: "Login"
            });
        }
    } else {
        //不需要登录
        next();
    }
});

//afterEach
router.afterEach((to, from) => {
    const store = mainStore();
    if (to.meta.title) {
        store.addHistory({
            title: to.meta.title,
            routeName: to.name
        });
    }

    if (from.meta.autoClose) {
        store.removeHisotryByRouteName(from.name);
    }

});
export default router;